package chenji.dailykit.ItemEditor;

import java.util.ArrayList;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import chenji.dailykit.Main;
import chenji.dailykit.ItemEditor.operationnode.OperationNode;
import chenji.dailykit.ItemEditor.operationnode.SimpleItemStack;
import chenji.dailykit.ItemEditor.operationnode.nodes.EnchantmentAddEditor;
import chenji.dailykit.ItemEditor.operationnode.nodes.EnchantmentEditor;
import chenji.dailykit.ItemEditor.operationnode.nodes.LoreEditor;
import chenji.dailykit.ItemEditor.operationnode.nodes.MenuNode;
import chenji.dailykit.inventory.SimpleInventory;

/**
 * @author ChenJi Copyright (c) 2016 ChenJi. All rights reserved.
 */
@SuppressWarnings("unused")
public class ItemEditor extends SimpleInventory {
	public OperationNode ON;
	private ItemStack item;
	private Player p;

	public void ChengeOperationNode(OperationNode newOn) {
		this.ON = newOn;
		newOn.setInventory(this.getInventory());
	}

	public ItemEditor(Player p, ItemStack i) {
		super(p, (short) 7, "DailyKit物品编辑器", 6);

		this.item = i;
		if (this.item.getAmount() != 1) {
			this.item.setAmount(1);
		}
		this.p = p;
		installItemStack();
	}

	@Override
	public void installItemStack() {
		this.getInventory().setItem(13, this.getItem());
		ItemStack ia = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 4);
		ItemMeta m = ia.getItemMeta();
		;
		m.setDisplayName("§c这里是目前物品的样式");
		ia.setItemMeta(m);
		int[] slot;
		slot = new int[] { 12, 14, 21, 22, 23 };
		for (int i : slot) {
			this.getInventory().setItem(i, ia);
		}
		ia = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 7);
		m = ia.getItemMeta();
		m.setDisplayName("§c↓↓↓↓↓↓§a操作面板§c↓↓↓↓↓↓");
		ia.setItemMeta(m);
		slot = new int[] { 19, 20, 24, 25 };
		for (int i : slot) {
			this.getInventory().setItem(i, ia);
		}
		/**
		 * 帮助物品
		 */
		ia = new SimpleItemStack(Material.ANVIL,
				new String[] { "§a→ §c这是DailyKit插件内置的一个简", "§a→ §c单的物品编辑器，可以很方便的修", "§a→ §c改一个物品的各项数据，方便礼包",
						"§a→ §c的制作。", "§a→ §c下方的一些图标可以定义了一些修", "§a→ §c改选项，点击后可以按照要求修改", "§a→ §c物品对应的数据。" },
				"§c→→→→→→→→→→§a帮助§c←←←←←←←←←←");
		this.getInventory().setItem(10, ia);
		ia = new SimpleItemStack(Material.NETHER_STAR, new String[] { "§c点击获取一个编辑器目前状态的物品" }, "§a→ §c获取物品");
		this.getInventory().setItem(11, ia);
		ia = new SimpleItemStack(Material.NETHER_STAR, new String[] { "§c点击获取十个编辑器目前状态的物品" }, "§a→ §c获取物品");
		ia.setAmount(10);
		this.getInventory().setItem(15, ia);
		ia = new SimpleItemStack(Material.BARRIER, new String[] { "§c点击并清理编辑器。" }, "§a→ §c获取物品");
		this.getInventory().setItem(16, ia);
		this.chengeMainMenuEditor();
	}

	public boolean setChat(String s) {
		return this.ON.setChat(s);
	}

	public void getChat() {
		Main.getItemEditorManager().getItemEditorEventManager().getPlayerChat(p);
	}

	public void setItem(ItemStack i) {
		this.getInventory().setItem(13, i);
		this.item = i;
	}

	public ItemStack getItem() {
		return this.item;
	}

	public void clickInventory(Player p2, int slot) {
		if (p2.equals(this.p)) {
			if ((slot >= 28 && slot <= 35) || (slot >= 38 && slot <= 45)) {
				this.ON.Click(slot);
				return;
			}
			if (slot == 11) {
				this.p.getInventory().addItem(this.item.clone());
				return;
			}
			if (slot == 15) {
				ItemStack i = this.getItem().clone();
				i.setAmount(10);
				this.p.getInventory().addItem(i);
				return;
			}
			if (slot == 16) {
				Main.getItemEditorManager().removeEditor(p);
				this.p.closeInventory();
				return;
			}
			if (this.ON.getShowPageItem()) {
				if (slot == 46) {
					this.ON.downPage();
					return;
				}
				if (slot == 52) {
					this.ON.upPage();
					return;
				}
				return;
			}
		} else {
			p2.closeInventory();
			return;
		}
	}

	public void printChengePage() {
		this.getInventory().setItem(46, Main.getOperationNodeItemLibrary().getUpPage());
		this.getInventory().setItem(52, Main.getOperationNodeItemLibrary().getNextPage());
		return;
	}

	public void chengeMainMenuEditor() {
		this.clearOperationNode();
		this.ON = new MenuNode(this.item, this, this.p);
		this.ON.setInventory(getInventory());
	}

	public void chengeLoreEditor() {
		this.clearOperationNode();
		this.ON = new LoreEditor(this.item, this, this.p);
		this.ON.setInventory(getInventory());
	}

	public void clearOperationNode() {
		ItemStack item = new ItemStack(Material.AIR);
		for (int i = 28; i <= 34; i++) {
			this.getInventory().setItem(i, item);
		}
		for (int i = 37; i <= 43; i++) {
			this.getInventory().setItem(i, item);
		}
	}

	public void chengeEnchantmentEditor() {
		this.clearOperationNode();
		this.ON = new EnchantmentEditor(this.item, this, this.p);
		this.ON.setInventory(getInventory());
		this.getInventory().setItem(52, Main.getOperationNodeItemLibrary().getFillItem());
		this.getInventory().setItem(46, Main.getOperationNodeItemLibrary().getFillItem());
	}

	public void chengeEnchantmentAddEditor() {
		this.clearOperationNode();
		this.ON = new EnchantmentAddEditor(this.item, this, this.p);
		this.ON.setInventory(this.getInventory());
		this.printChengePage();
	}
}
